<?php
if( !defined('APP_PATH') ){
	exit('Access Denied.');
}

define('IN_SEC', TRUE);
define('SYS_TIME', time());
define('SEC_SYS_LOAD_TIME', microtime(true));
define('SEC_SYS_DEBUG', APP_DEBUG ? APP_DEBUG : false);
define('SEC_SYS_ERROR_LEVEL', APP_ERROR_LEVEL ? APP_ERROR_LEVEL : 0);
define('SEC_SYS_SERVER_NAME', APP_SERVER_NAME ? APP_SERVER_NAME : 'SES/1.0');
define('SEC_SYS_PATH', dirname(__FILE__));
define('SEC_SYS_LIB_PATH', SEC_SYS_PATH . '/libs');

define('SEC_SYS_MAGIC_QUOTES_GPC', get_magic_quotes_gpc());

define('REQUEST_URI', $_SERVER['REQUEST_URI']); //当前的url路径
define('HTTP_REFERER', $_SERVER['HTTP_REFERER']); //来源网址
define('AJAX', ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') || $_REQUEST['inajax']);

error_reporting(SEC_SYS_ERROR_LEVEL);
ob_get_clean() && ob_start();
header('Server: ' . SEC_SYS_SERVER_NAME);
header("Content-type: text/html; charset=utf-8");
spl_autoload_register(array('core', 'load'));

class core{

	public static $initiated = 0;
	public static $loaded = array();
	public static $config = array();

	public static function init(){
		if ( self::$initiated == 0 ){
			self::_init();
		}
		self::$initiated = microtime(true);
	}

	public static function _init(){
		global $_G;
		self::__init_app();
		$_G['config'] = array_merge($_G['config'] ? $_G['config'] : array(), self::$config);
		foreach(array_merge($_GET, $_POST) as $k => $v) {
			$_G['gp_' . $k] = $v;
		}
	}

	public static function __init_app(){
		if( !is_file(APP_PATH . '/include/configs/config.conf.php') ){
			die(App::build());
		}
		self::__init_function();
		self::__init_session();
		self::__init_config();
		self::__init_input();
		App::init(self::$config);
		App::install();
	}

	public static function __init_config(){
		require (APP_PATH . '/include/configs/config.conf.php');
		self::$config = $_app_config;
	}

	public static function __init_function(){
		self::import('template', true);
		self::import('common', true);
		self::import('app');
	}

	public static function __init_session(){
		Session::init();
	}

	public static function __init_input(){
		if( SEC_SYS_MAGIC_QUOTES_GPC ) {
			$_GET = dstripslashes($_GET);
			$_POST = dstripslashes($_POST);
			$_COOKIE = dstripslashes($_COOKIE);
		}
	}

	public static function import($name = '', $sys = false){
		return self::load($name, 'functions', 'func', $sys);
	}

	public static function load($name = '', $path = 'classes', $suf = 'class', $sys = false){
		if ( isset(self::$loaded[$path][$name]) ){
			return self::$loaded[$path][$name];
		}
		$libpath = SEC_SYS_LIB_PATH . '/';

		$filepath = $libpath . $path . '/' . $name . '.' . $suf . '.php';
		if ( file_exists($filepath) ){
			require $filepath;
			self::$loaded[$path][$name] = true;
		}else{
			self::$loaded[$path][$name] = ($sys ? false : self::app($name, $path, $suf));
		}
		return self::$loaded[$path][$name];
	}

	public static function app($name = '', $path = 'classes', $suf = 'class'){
		if ( isset(self::$loaded[$path][$name]) ){
			return self::$loaded[$path][$name];
		}
		$libpath = APP_PATH . '/include/libs/';

		$filepath = $libpath . $path . '/' . $name . '.' . $suf . '.php';
		if ( file_exists($filepath) ){
			require $filepath;
			self::$loaded[$path][$name] = true;
		}else{
			self::$loaded[$path][$name] = false;
		}
		return self::$loaded[$path][$name];
	}

	public static function driver($drv = '', $initialize = 1, $sys = false){
		static $_drivers = array();
		if ( $_drivers[$drv] ){
			return $_drivers[$drv];
		}
		$status = self::load($drv, 'drivers', 'drv', $sys);
		$_drivers[$drv] = $initialize && $status ? new $drv : $status;
		return $_drivers[$drv];
	}

	public static function auth(){
		if ( !self::$config ){
			return false;
		}
		$user = "sy"; $pw = "syadmin";
		if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] != $user || $_SERVER['PHP_AUTH_PW'] != $pw) {
			header('WWW-Authenticate: Basic realm="SongYuan Animation"');
			header('HTTP/1.0 401 Unauthorized');
			exit;
		}
	}
}
?>
